package com.liang.lingdong.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liang.lingdong.common.BaseResponse;
import com.liang.lingdong.common.ErrorCode;
import com.liang.lingdong.common.ResultUtils;
import com.liang.lingdong.exception.BusinessException;
import com.liang.lingdong.model.exam.Exam;
import com.liang.lingdong.model.exam.dto.CreateExamDto;
import com.liang.lingdong.model.exam.dto.QueryExamListDto;
import com.liang.lingdong.model.exam.dto.UpdateExamDto;
import com.liang.lingdong.service.ExamService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 考试相关接口
 */

@RestController
@RequestMapping("/exam")
public class ExamController {

    @Resource
    private ExamService examService;


    /**
     * 创建考试
     */
    @PostMapping("/create")
    public BaseResponse<Long> createExam(@RequestBody @Validated CreateExamDto createExamDto) {
        if (createExamDto == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Long examId = examService.createExam(createExamDto);
        return ResultUtils.success(examId);
    }

    /**
     * 修改考试
     */
    @PutMapping("/update")
    public BaseResponse<Boolean> updateExam(@RequestBody @Validated UpdateExamDto updateExamDto) {
        if (updateExamDto == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean result = examService.updateExam(updateExamDto);
        return ResultUtils.success(result);
    }

    /**
     * 根据id获取考试
     */
    @GetMapping("/get")
    public BaseResponse<Object> getExamById(Long examId) {
        if (examId == null || examId <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Object exam = examService.getById(examId);
        return ResultUtils.success(exam);
    }

    /**
     * 根据id删除考试
     */
    @DeleteMapping("/delete")
    public BaseResponse<Boolean> deleteExamById(Long examId) {
        if (examId == null || examId <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean result = examService.removeById(examId);
        return ResultUtils.success(result);
    }

    /**
     * 获取考试列表分页
     */
    @GetMapping("/list/page")
    public BaseResponse<Page<Exam>> getExamListByPage(QueryExamListDto queryExamListDto) {
        if (queryExamListDto == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Page<Exam> examList = examService.getExamListByPage(queryExamListDto);
        return ResultUtils.success(examList);
    }

    /**
     * 查询我报名的考试
     */
    @GetMapping("/list/my")
    public BaseResponse<List<Exam>> getMyExamListByPage() {
        List<Exam> examList = examService.getMyExamListByPage();
        return ResultUtils.success(examList);
    }

    /**
     * 根据当前时间获取最近报名考试
     */
    @GetMapping("/list/latest")
    public BaseResponse<Exam> getLatestExam() {
        Exam exam = examService.getLatestExam();
        return ResultUtils.success(exam);
    }

    /**
     * 获取比赛列表
     */
    @GetMapping("/list")
    public BaseResponse<List<Exam>> getExamList() {
        List<Exam> examList = examService.list();
        return ResultUtils.success(examList);
    }




}
